﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Agenda.Helpers;
using Agenda.Forms.Common;
using System.Data.OleDb;
using Agenda.Service;
using Agenda.Entities;

namespace Agenda.Forms.Obras
{
    public partial class Agregar : BaseForm
    {
        public List<Control> myControls = new List<Control>();

        public Agregar()
        {
            InitializeComponent();
        }

        private void Agregar_Load(object sender, EventArgs e)
        {
            CargarTextos();
            DataGridView1_Load();
        }

        private void CargarTextos()
        {
            label1.Text = "Tipo de Obra";
            label2.Text = "Obra";
            label3.Text = "Número de Contrato";
            label4.Text = "Nombre de la Obra";
            comboBox1.Text = "Seleccione el Tipo de Obra";
            comboBox2.Text = "Seleccione una Obra";
            comboBox3.Text = "País";
            comboBox4.Text = "Provincia";
            textBox3.Text = "Ciudad";
            button1.Text = "Crear";
            button2.Text = "Cerrar";
            textBox4.Text = "Diametro";
            textBox5.Text = "Presión";
            textBox6.Text = "Rigidez";
            textBox7.Text = "Longitud";
            label8.Text = "Comitente";
            label9.Text = "Empresa Constructora";
            label10.Text = "Subcontratista";
            label11.Text = "Inspección";
            label12.Text = "Inicio de Obra";
            label13.Text = "Fin de Obra";
            label14.Text = "Monto";
        }

        private void comboBox1_TextUpdate(object sender, EventArgs e)
        {
            if (comboBox1.Text == "Otek Servicios")
            {
                CargarCamposServicios();
            }
            else{CargarCamposAmitech();}
        }

        private void CargarCamposAmitech()
        {
            comboBox2.Text = "Seleccione una Obra";
            comboBox2.Items.Clear();
            comboBox2.Items.Add("Obra Aérea");
            comboBox2.Items.Add("Obra Enterrada");
        }

        private void CargarCamposServicios()
        {
            comboBox2.Text = "Seleccione una Obra";
            comboBox2.Items.Clear();
            comboBox2.Items.Add("Tanque");
            comboBox2.Items.Add("Revestimiento");
            comboBox2.Items.Add("Instalación");
            comboBox2.Items.Add("Jhoint Tester");
        }

        private void comboBox3_SelectedValueChanged(object sender, EventArgs e)
        {
            if (comboBox3.Text == "Argentina")
            {
                CargarCamposArgentina();
            }

            if (comboBox3.Text == "Brasil")
            {
                CargarCamposBrasil();
            }

            if (comboBox3.Text == "Chile")
            {
                CargarCamposChile();
            }

            if (comboBox3.Text == "Venezuela")
            {
                cargarCamposVenezuela();
            }
        }

        private void cargarCamposVenezuela()
        {
            ReinicioCampos();
        }

        private void CargarCamposChile()
        {
            ReinicioCampos();
        }

        private void CargarCamposBrasil()
        {
            ReinicioCampos();
        }

        private void CargarCamposArgentina()
        {
            ReinicioCampos();
            comboBox4.Items.Add("Buenos Aires");
            comboBox4.Items.Add("Catamarca");
            comboBox4.Items.Add("Córdoba");
            comboBox4.Items.Add("Corrientes");
            comboBox4.Items.Add("Chaco");
            comboBox4.Items.Add("Chubut");
            comboBox4.Items.Add("Entre Ríos");
            comboBox4.Items.Add("Formosa");
            comboBox4.Items.Add("Jujuy");
            comboBox4.Items.Add("La Pampa");
            comboBox4.Items.Add("La Rioja");
            comboBox4.Items.Add("Mendoza");
            comboBox4.Items.Add("Misiones");
            comboBox4.Items.Add("Neuquén");
            comboBox4.Items.Add("Río Negro");
            comboBox4.Items.Add("Salta");
            comboBox4.Items.Add("San Juan");
            comboBox4.Items.Add("San Luis");
            comboBox4.Items.Add("Santa Cruz");
            comboBox4.Items.Add("Santa Fe");
            comboBox4.Items.Add("Santiago del Estero");
            comboBox4.Items.Add("Tierra del Fuego");
            comboBox4.Items.Add("Tucuman");
        }

        private void ReinicioCampos()
        {
            comboBox4.Text = "Provincia";
            textBox3.Text = "Ciudad";
        }

        private void comboBox4_SelectedValueChanged(object sender, EventArgs e)
        {
            textBox3.Text = "Ciudad";
        }

        #region LimpiarCampos
        private void textBox3_MouseClick(object sender, MouseEventArgs e)
        {
            textBox3.Text = "";
        }

        private void textBox4_MouseClick(object sender, MouseEventArgs e)
        {
            textBox4.Text = "";
        }

        private void textBox5_MouseClick(object sender, MouseEventArgs e)
        {
            textBox5.Text = "";
        }

        private void textBox6_MouseClick(object sender, MouseEventArgs e)
        {
            textBox6.Text = "";
        }

        private void textBox7_MouseClick(object sender, MouseEventArgs e)
        {
            textBox7.Text = "";
        }

        #endregion

        #region ReestablecerCampos

        #region Diametro
        private void textBox4_Leave(object sender, EventArgs e)
        {
            if (textBox4.Text == "")
            {
                textBox4.Text = "Diametro";
            }
        }

        #endregion Diametro

        #region Presión
        private void textBox5_Leave(object sender, EventArgs e)
        {
            if (textBox5.Text == "")
            {
                textBox5.Text = "Presión";
            }
        }

        #endregion

        #region Rigidez
        private void textBox6_Leave(object sender, EventArgs e)
        {
            if (textBox6.Text == "")
            {
                textBox6.Text = "Rigidez";
            }
        }

        #endregion

        #region Longitud
        private void textBox7_Leave(object sender, EventArgs e)
        {
            if (textBox7.Text == "")
            {
                textBox7.Text = "Longitud";
            }
        }

        #endregion

        #endregion

        #region Validaciones
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsLetter(e.KeyChar) || char.IsWhiteSpace(e.KeyChar) || char.IsSymbol(e.KeyChar))
            {
                MessageBox.Show("Solo se permiten números.");
                e.Handled = true;
                return;
            }
        }
        private void textBox20_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsLetter(e.KeyChar) || char.IsWhiteSpace(e.KeyChar) || char.IsSymbol(e.KeyChar))
            {
                MessageBox.Show("Solo se permiten números.");
                e.Handled = true;
                return;
            }
        }

        #endregion

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Obra obra = new Obra();
            obra.obraType = comboBox1.Text;
            obra.style = comboBox2.Text;
            obra.contractNumber = Convert.ToInt32(textBox1.Text);
            obra.obraName = textBox2.Text;
            obra.country = comboBox3.Text;
            obra.province = comboBox4.Text;
            obra.city = textBox3.Text;
            obra.comitente = textBox24.Text;
            obra.constructionCompany = textBox25.Text;
            obra.subContractor = textBox26.Text;
            obra.inspection = textBox27.Text;
            //obra.initiation = Convert.ToString(dateTimePicker1);
            //dateTimePicker1.Format = DateTimePickerFormat.Custom;
            //dateTimePicker1.CustomFormat = "dd'/'MM'/'yyyy";
            obra.initiation = dateTimePicker1.Value.ToShortDateString();
            obra.finish = dateTimePicker2.Value.ToShortDateString();
            obra.amount = Convert.ToInt32(textBox28.Text);
            
            ObraService ob = new ObraService();
            //cuando creo, que me devuelva el id con el que lo creo
            int currentObraId = ob.Add(obra);

            int rowCant = dataGridView1.RowCount;
            int i = 0;
            while (i < rowCant)
            {
                Spout spout = new Spout();
                spout.diameter = Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value);
                spout.pressure = Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value);
                spout.rigidity = Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value);
                spout.length = Convert.ToInt32(dataGridView1.Rows[i].Cells[3].Value);
                spout.obraId = currentObraId;
                SpoutService sp = new SpoutService();
                sp.Add(spout);
                i++;
            }

            MessageBox.Show("La obra ha sido agendada con éxito");
            this.Close();
        }

        private void textBox24_TextChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Add(textBox4.Text, textBox5.Text, textBox6.Text, textBox7.Text, "Borrar");
            textBox4.ResetText();
            textBox5.ResetText();
            textBox6.ResetText();
            textBox7.ResetText();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex].CellType.Name == new DataGridViewButtonColumn().CellType.Name)
            {
                if (dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString() == "Borrar")
                {
                    dataGridView1.Rows.RemoveAt(e.RowIndex);
                    dataGridView1.Refresh();
                }
            }
        }

        private void DataGridView1_Load()
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.Columns.Add(CreateColumn("Diametro", "Column1", 150));
            dataGridView1.Columns.Add(CreateColumn("Presión", "Column2", 150));
            dataGridView1.Columns.Add(CreateColumn("Rigidez", "Column3", 150));
            dataGridView1.Columns.Add(CreateColumn("Longitud", "Column4", 150));
            dataGridView1.Columns.Add(new DataGridViewButtonColumn());
        }

        private DataGridViewColumn CreateColumn(string header, string prop, int width)
        {
            DataGridViewColumn res = new DataGridViewTextBoxColumn();
            res.HeaderText = header;
            res.DataPropertyName = prop;
            res.Width = width;

            return res;
        }

    }
}
